package com.hnucm.c201901020241.my;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.hnucm.c201901020241.R;
import com.hnucm.c201901020241.discuss.Discuss_post_information;
import com.hnucm.c201901020241.discuss.Disscuss_item;
import com.hnucm.c201901020241.pojo.Content;
import com.hnucm.c201901020241.pojo.UserShoucang;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class Public extends AppCompatActivity {
    RecyclerView recyclerView;
    List<Content>contentList;
    MyAdapter myAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_public);
        recyclerView=findViewById(R.id.recyclerView3);
        contentList=new ArrayList<>();
        myAdapter=new MyAdapter();
        x.Ext.init(getApplication());
        SharedPreferences sharedPreferences=getSharedPreferences("user",MODE_MULTI_PROCESS);//MODE_PRIVATE私有权限，保护文件
        String account = sharedPreferences.getString("account", "false");
        RequestParams requestParams= new RequestParams("http://121.4.27.254:8090/queryContentListbyaccount?");
        requestParams.addQueryStringParameter("account",account);
        x.http().get(requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.i("public",result);
                try {
                    JSONArray jsonArray=new JSONArray(result);
                    for(int i=0;i<jsonArray.length();i++){
                        Content content=new Content();
                        JSONObject jsonObject=jsonArray.getJSONObject(i);
                        content.title=jsonObject.getString("title");
                        content.name=jsonObject.getString("name");
                        content.time=jsonObject.getString("time");
                        content.account=jsonObject.getString("account");
                        content.base64=jsonObject.getString("base64");
                        content.title_discuss=jsonObject.getString("title_discuss");
                        content.minformation=jsonObject.getString("minformation");
                        content.comments=jsonObject.getInt("comments");
                        contentList.add(content);
                    }
                    recyclerView.setAdapter(myAdapter);
                    recyclerView.setLayoutManager(new LinearLayoutManager(Public.this));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });


        findViewById(R.id.imageView19).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        //        刷新
        RefreshLayout refreshLayout = (RefreshLayout)findViewById(R.id.SmartRefreshLayout6);
        refreshLayout.setRefreshHeader(new ClassicsHeader(this));
        refreshLayout.setRefreshFooter(new ClassicsFooter(this));
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                refreshLayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
            }
        });

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                contentList.clear();
                x.Ext.init(getApplication());
                RequestParams requestParams= new RequestParams("http://121.4.27.254:8090/queryContentListbyaccount?");
                requestParams.addQueryStringParameter("account",account);
                x.http().get(requestParams, new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        Log.i("public",result);
                        try {
                            JSONArray jsonArray=new JSONArray(result);
                            for(int i=0;i<jsonArray.length();i++){
                                Content content=new Content();
                                JSONObject jsonObject=jsonArray.getJSONObject(i);
                                content.title=jsonObject.getString("title");
                                content.name=jsonObject.getString("name");
                                content.time=jsonObject.getString("time");
                                content.account=jsonObject.getString("account");
                                content.base64=jsonObject.getString("base64");
                                content.title_discuss=jsonObject.getString("title_discuss");
                                content.minformation=jsonObject.getString("minformation");
                                content.comments=jsonObject.getInt("comments");
                                contentList.add(content);
                                System.out.println(content.comments);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        myAdapter.notifyDataSetChanged();
                        refreshLayout.finishRefresh(2000/*,false*/);//传入false表示加载失败
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {

                    }

                    @Override
                    public void onCancelled(CancelledException cex) {

                    }

                    @Override
                    public void onFinished() {

                    }
                });

            }
        });

    }
    public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(Public.this).inflate(R.layout.public_item, parent, false);
            MyViewHolder myViewHolder = new MyViewHolder(view);
            return myViewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            holder.titleTv.setText(contentList.get(position).title);
            holder.title_discussTv.setText(contentList.get(position).title_discuss);
            holder.timeTv.setText(contentList.get(position).time);
            holder.minformationTv.setText(contentList.get(position).minformation);
            holder.commentsTv.setText(contentList.get(position).comments+"");
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    从这里将当前文章标题和当前论坛标题，发帖人作为参数传值，在数据库中建立一个表来记录每一个帖子中的详细回复信息
//                    数据库中建表，搜索的数据要和文章标题，当前论坛标题，发帖人一致才是对应的数据
//                    然后对于每一个人再开一个表，存自己发的帖子，
                    Intent intent=new Intent(Public.this, Discuss_post_information.class);
                    intent.putExtra("title_discuss",contentList.get(position).title_discuss);
                    intent.putExtra("name",contentList.get(position).name);
                    intent.putExtra("time",contentList.get(position).time);
                    intent.putExtra("account",contentList.get(position).account);
                    intent.putExtra("mainInformation",contentList.get(position).minformation);
                    intent.putExtra("base64",contentList.get(position).base64);
                    startActivity(intent);
                }
            });
        }
        @Override
        public int getItemCount() {
            return contentList.size();
        }
    }


    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView titleTv,title_discussTv,timeTv,minformationTv,commentsTv;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            titleTv=itemView.findViewById(R.id.textView70);
            title_discussTv=itemView.findViewById(R.id.textView41);
            timeTv=itemView.findViewById(R.id.textView71);
            minformationTv=itemView.findViewById(R.id.textView43);
            commentsTv=itemView.findViewById(R.id.textView42);
        }
    }
}